package com.example.my_isc_nirscan.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.my_isc_nirscan.R;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ArticleActivity  extends AppCompatActivity {

    private static final String PREF_NAME = "ReadingProgress";
    private static final String KEY_SCROLL_POSITION = "scrollPosition";
    private ScrollView scrollView;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.article);


        // 初始化 Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar_article);
        if (toolbar == null) {
            throw new IllegalStateException("Toolbar with ID 'toolbar_article' not found in the layout");
        }

        // 设置 Toolbar 为 ActionBar
        setSupportActionBar(toolbar);

        // 设置动态标题
        String dynamicTitle = "Dynamic Title"; // 这里可以替换为动态获取的标题
        setTitle(dynamicTitle);

        // 启用返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        // 初始化SharedPreferences
        preferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);

        // 找到ScrollView和TextView控件
        scrollView = findViewById(R.id.scrollView);
        TextView textView = findViewById(R.id.textView);

        try {
            // 打开assets目录下的文件
            InputStream inputStream = getAssets().open("article.txt");
            // 创建输入流读取器
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            // 创建缓冲读取器
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            StringBuilder stringBuilder = new StringBuilder();
            String line;

            // 逐行读取文本
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }

            // 关闭读取器
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();

            // 设置文本到TextView
            textView.setText(stringBuilder.toString());

            // 恢复上次阅读位置
            scrollView.post(() -> {
                int savedPosition = preferences.getInt(KEY_SCROLL_POSITION, 0);
                scrollView.scrollTo(0, savedPosition);
            });

        } catch (IOException e) {
            e.printStackTrace();
            textView.setText("读取文件出错: " + e.getMessage());
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
//        onBackPressed();
        // 自定义返回按钮逻辑
        finish(); // 关闭当前活动
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onSupportNavigateUp();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 保存当前阅读位置
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(KEY_SCROLL_POSITION, scrollView.getScrollY());
        editor.apply();
    }
}